import request from '@/libs/request'

var _self;
export const index =  {
	name: 'dataListPopup',
	props: {
		show:{type:Boolean,default:false},
		dataInfo:{type:Object,default:{}},
		url:{type:String,default:""},
		dataItem:{type:Array,default:[]},
		selecNum:{type:Number,default:1},
		
	},
	watch:{
		popupShow(val){
			this.$emit('update:show', val);
			this.popupOpt();
		},
		
		show(val) {
			this.initData();
			this.popupShow = val;
			this.popupOpt();
		},

		selectList(val){
			
			this.$emit('update:dataInfo', this.selectItem);
		},
		
		dataInfo(val) {
			this.selectItem = val;
		}
	},
	data() {
		return {
			params:{
				page: 1,
				pageSize: 20,
			},

			selectItem:{},
			selectList:[],
			dataList:[],
			popupShow:this.show,
			scrollHeight:uni.getSystemInfoSync().windowHeight*0.6+"px",
			icons:[
				{type:"search",size:uni.upx2px(30)},
				{type:"camera",size:uni.upx2px(30)},
				{type:"search",size:uni.upx2px(40)},
				{type:"checkmarkempty",size:uni.upx2px(60)},
			],
			dateNow: new Date().Format("yyyy-MM-dd"),
		}
	},
	created() {
		_self=this;
	},
	
	methods:{
		nextPage:function(e){
			this.params.page+=1;
			this.initData();
		},
		
		initData: async function(){
			
			let retData=await this.getList(this.params);
			if(retData["dataList"]) retData=retData["dataList"];
			if(this.params.page==1){
				this.dataList=retData;
			}else{
				if(retData.length==0) this.params.page-=1;
				this.dataList.push.apply(this.dataList,retData);
			}
			
		},
		
		/**
		 * 返回列表数据
		 * @param {Object} data
		 */
		getList:function(data) {
			return request({url: this.url,method: 'POST',data});
		},
		
		change:function(e) {
			this.popupShow=e.show;
		},
		
		popupOpt:function(show){
			if(this.popupShow){
				this.$refs.popup.open('bottom');
			}else{
				this.$refs.popup.close();
			}
		},
		searchInfo:function(show){
			this.initData();
		},
		selectInfo:function(index){
			
			let key=this.dataItem[1]['val'];
			let itemKey=this.dataList[index][key];
			if(this.selecNum==1){
				
			}else{
				if(this.selectList.length>=this.selecNum&&!this.selectItem[itemKey]) return true;
			}
			if(this.selectItem[itemKey]){
				let key=this.selectList.indexOf(this.selectItem[itemKey][key]);
				this.selectList.splice(key, 1); 
				if(this.selecNum==1){
					this.selectItem={};
				}else{
					delete this.selectItem[itemKey];
				}
				
			}else{
				
				if(this.selecNum==1){
					this.selectList=[this.dataList[index][key]];
					this.selectItem=this.dataList[index];
				}else{
					this.selectList.push(this.dataList[index][key]);
					this.selectItem[itemKey]=this.dataList[index];
				}
				
			}
			
			console.log(this.selectItem);
		
		},
		
		confirmSelect:function(){
			this.$refs.popup.close();
		}
		

	}
		

}